extends KinematicBody2D


# 角色战斗属性
var max_hp = 100 # 最大生命值
var bomb_max = 1 # 当前最大炸弹数量
var bomb_range = 1 # 当前炸弹爆炸范围
var move_speed = 1 # 当前炸弹爆炸范围

var now_hp = 100 # 当前生命值

# 角色属性

var base_speed = 500 # 基础移动速度
var add_speed = 100 # 每个加速度
var kick = false # 是否可以踢飞炸弹？
var invincible = false #是否无敌? (通常在复活时)
var confusion = false # 是否混乱？ (移动颠倒)
var DIRECTION = 1 #移动方向，1为向左
var velocity = Vector2.ZERO

var player_map_pos = Vector2.ZERO

onready var hp_bar = $HPBar

func init_player():
	##初始化玩家
	max_hp = 100
	bomb_max = 1
	bomb_range = 1
	move_speed = 1
	now_hp = max_hp
	update_hp()
	
func _ready():
	
	pass

func _physics_process(delta):
	move_state(delta)
	pass
	
	
func move_state(delta):
	#移动
	var input_vector = Vector2.ZERO
	input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	
	input_vector = input_vector.normalized() * 0.5  * GdGlobal.TILE_SIZE * delta * (base_speed + move_speed * add_speed)
	
	velocity = move_and_slide(input_vector)
	if confusion:
		velocity = -velocity

func reflash_player_map_pos():
	player_map_pos = GdGlobal.UIBattle.world_to_map(self.get_position())
	pass


func set_bomb():
	## 放炸弹
	var bomb = GdGlobal.bomb_scene.instance()
	bomb.set_bomb_range(bomb_range)
	GdGlobal.UIBattle.bomb_sort.add_child(bomb)
	bomb.set_pos_and_update(GdGlobal.UIBattle.tile_center_position(self.get_position()))
	bomb.creater_fire_pos_array()
	
func _input(_event):
	if Input.is_action_pressed("ui_accept") :
		reflash_player_map_pos()
		if GdGlobal.UIBattle.bomb_sort.get_child_count() < bomb_max && GdGlobal.UIBattle.old_bomb_pos != player_map_pos:
			#print(GdGlobal.UIBattle.bomb_sort.get_child_count() )
			set_bomb()
			
func update_hp():
	#更新血条
	hp_bar.value = (now_hp*100.0)/(max_hp*100.0) *100.0
#	print(now_hp)
#	print(hp_bar.value)
	
func player_hurn(value):
	#受伤
	now_hp-=value
	update_hp()
	if now_hp<=0:
		GdGlobal.UIBattle.gameover()
	pass
